home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / TUT02NEW.ZIP / TUT2.TXT < prev   
Text File  |  1994-11-29  |  15KB  |  409 lines

  1.                    ╒═══════════════════════════════╕
  2.                    │         W E L C O M E         │
  3.                    │  To the VGA Trainer Program   │ │
  4.                    │              By               │ │
  5.                    │      DENTHOR of ASPHYXIA      │ │ │
  6.                    │      (updated by Snowman)     │ │ │
  7.                    ╘═══════════════════════════════╛ │ │
  8.                      ────────────────────────────────┘ │
  9.                        ────────────────────────────────┘
  10.  
  11.                            --==[ PART 2 ]==--
  12.  
  13.  
  14.  
  15. [Note: things in brackets have been added by Snowman.  The original text
  16. has remained mostly unaltered except for the inclusion of C++ material]
  17.  
  18. ■ Introduction
  19.  
  20. Hi there again! This is Grant Smith, AKA Denthor of ASPHYXIA. This is the
  21. second part of my Training Program for new programmers. I have only had a
  22. lukewarm response to my first part of the trainer series ... remember, if
  23. I don't hear from you, I will assume that you are all dead and will stop
  24. writing the series ;-). Also, if you do get in contact with me I will give
  25. you some of our fast assembly routines which will speed up your demos no
  26. end. So go on, leave mail to GRANT SMITH in the main section of the
  27. MailBox BBS, start up a discussion or ask a few questions in this Conference,
  28. leave mail to ASPHYXIA on the ASPHYXIA BBS, leave mail to Denthor on
  29. Connectix, or write to Grant Smith,
  30.                        P.O.Box 270
  31.                        Kloof
  32.                        3640
  33. See, there are many ways you can get in contact with me! Use one of them!
  34.  
  35. In this part, I will put the Pallette through it's paces. What the hell is
  36. a pallette? How do I find out what it is? How do I set it? How do I stop
  37. the "fuzz" that appears on the screen when I change the pallette? How do
  38. I black out the screen using the pallette? How do I fade in a screen?
  39. How do I fade out a screen? Why are telephone calls so expensive?
  40. Most of these quesions will be answered in this, the second part of my
  41. Trainer Series for Pascal.
  42.  
  43.  
  44. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  45. ■  What is the Pallette?
  46.  
  47. A few weeks ago a friend of mine was playing a computer game. In the game
  48. there was a machine with stripes of blue running across it. When the
  49. machine was activated, while half of the the blue stripes stayed the same,
  50. the other half started to change color and glow. He asked me how two stripes
  51. of the same color suddenly become different like that. The answer is simple:
  52. the program was changing the pallette.
  53.  
  54. As you know from Part 1, there are 256 colors in MCGA mode, numbered 0 to 255.
  55. What you don't know is that each if those colors is made up of different
  56. intensities of Red, Green and Blue, the primary colors (you should have
  57. learned about the primary colors at school). These intensities are numbers
  58. between 0 and 63. The color of bright red would for example be obtained by
  59. setting red intensity to 63, green intensity to 0, and blue intensity to 0.
  60. This means that two colors can look exactly the same, eg you can set color 10
  61. to bright red and color 78 to color bright red. If you draw a picture using
  62. both of those colors, no-one will be able to tell the difference between the
  63. two.. It is only when you again change the pallette of either of them will
  64. they be able to tell the difference.
  65.  
  66. Also, by changing the whole pallette, you can obtain the "Fade in" and "Fade
  67. out" effects found in many demos and games. Pallette manipulation can become
  68. quite confusing to some people, because colors that look the same are in fact
  69. totally seperate.
  70.  
  71.  
  72. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  73. ■  How do I read in the pallette value of a color?
  74.  
  75. This is very easy to do. To read in the pallette value, you enter in the
  76. number of the color you want into port $3c7 [0x03C7], then read in the values
  77. of red, green and blue respectively from port $3c9 [0x03C9]. Simple, huh? Here
  78. is a procedure that does it for you :
  79.  
  80. [Pascal]
  81.  
  82. Procedure GetPal(ColorNo : Byte; Var R,G,B : Byte);
  83.   { This reads the values of the Red, Green and Blue values of a certain
  84.     color and returns them to you. }
  85. Begin
  86.    Port[$3c7] := ColorNo;
  87.    R := Port[$3c9];
  88.    G := Port[$3c9];
  89.    B := Port[$3c9];
  90. End;
  91.  
  92. [C++]
  93.  
  94. void GetPal(unsigned char ColorNo, unsigned char &R,
  95.         unsigned char &G,      unsigned char &B) {
  96.  
  97.   outp (0x03C7,ColorNo); // here is the pallette color I want read
  98.   R = inp (0x03C9);
  99.   G = inp (0x03C9);
  100.   B = inp (0x03C9);
  101.  
  102. }
  103.  
  104. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  105. ■  How do I set the pallette value of a color?
  106.  
  107. This is also as easy as 3.1415926535897932385. What you do is you enter in the
  108. number of the color you want to change into port $3c8 [0x03C8], then enter the
  109. values of red, green and blue respectively into port $3c9 [0x03C9]. Because
  110. you are all so lazy I have written the procedure for you ;-)
  111.  
  112. [Pascal]
  113.  
  114. Procedure Pal(ColorNo : Byte; R,G,B : Byte);
  115.   { This sets the Red, Green and Blue values of a certain color }
  116. Begin
  117.    Port[$3c8] := ColorNo;
  118.    Port[$3c9] := R;
  119.    Port[$3c9] := G;
  120.    Port[$3c9] := B;
  121. End;
  122.  
  123. [C++]
  124.  
  125. void Pal(unsigned char ColorNo, unsigned char R,
  126.      unsigned char G,       unsigned char B) {
  127.  
  128.   outp (0x03C8,ColorNo); // here is the pallette color I want to set
  129.   outp (0x03C9,R);
  130.   outp (0x03C9,G);
  131.   outp (0x03C9,B);
  132.  
  133. }
  134.  
  135. Asphyxia doesn't use the above pallete procedures, we use assembler versions,
  136. which will be given to PEOPLE WHO RESPOND TO THIS TRAINER SERIES (HINT,
  137. HINT)
  138.  
  139.  
  140. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  141. ■  How do I stop the "fuzz" that appears on my screen when I change the
  142.         pallette?
  143.  
  144. If you have used the pallette before, you will have noticed that there is
  145. quite a bit of "fuzz" on the screen when you change it. The way we counter
  146. this is as follows : There is an elctron beam on your monitor that is
  147. constantly updating your screen from top to bottom. As it gets to the
  148. bottom of the screen, it takes a while for it to get back up to the top of
  149. the screen to start updating the screen again.
  150.  
  151. The period where it moves from the bottom to the top is called the Verticle
  152. Retrace. During the verticle retrace you may change the pallette without
  153. affecting what is on the screen.
  154.  
  155. What we do is that we wait until a verticle retrace has started by calling a
  156. certain procedure; this means that everything we do now will only be shown
  157. after the verticle retrace, so we can do all sorts of strange and unusual
  158. things to the screen during this retrace and only the results will be shown
  159. when the retrace is finished. This is way cool, as it means that when we
  160. change the pallette, the fuzz doesn't appear on the screen, only the result
  161. (the changed pallette), is seen after the retrace! Neat, huh? ;-) I have put
  162. the purely assembler WaitRetrace routine in the sample code that follows this
  163. message. Use it wisely, my son.
  164.  
  165. NOTE : WaitRetrace can be a great help to your coding ... code that fits
  166.        into one retrace will mean that the demo will run at the same
  167.        speed no matter what your computer speed (unless you are doing a lot
  168.        during the WaitRetrace and the computer is slooooow). Note that in
  169.        the following sample program and in our SilkyDemo, the thing will run
  170.        at the same speed whether turbo is on or off.
  171.  
  172.  
  173. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  174. ■  How do I black out the screen using the pallette?
  175.  
  176. This is basic : just set the Red, Green and Blue values of all colors to
  177. zero intensity, like so :
  178.  
  179. [Pascal]
  180.  
  181. Procedure Blackout;
  182.   { This procedure blackens the screen by setting the pallette values of
  183.     all the colors to zero. }
  184. VAR loop1:integer;
  185. BEGIN
  186.   WaitRetrace;
  187.   For loop1:=0 to 255 do
  188.     Pal (loop1,0,0,0);
  189. END;
  190.  
  191. [C++]
  192.  
  193. void Blackout() {
  194.  
  195.   WaitRetrace();
  196.   for (int loop1=0;loop1<256;loop1++)
  197.     Pal (loop1,0,0,0);
  198.  
  199. }
  200.  
  201. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  202. ■  How do I fade in a screen?
  203.  
  204. Okay, this can be VERY effective. What you must first do is grab the
  205. pallette into a variable, like so :
  206.  
  207.    [Pascal]   VAR Pall := Array [0.255,1..3] of BYTE;
  208.    [C++]      unsigned char Pall[256][3];
  209.  
  210. 0 to 255 is for the 256 colors in MCGA mode, 1 to 3 is red, green and blue
  211. intensity values;
  212.  
  213. [Pascal]
  214.  
  215. Procedure GrabPallette;
  216. VAR loop1:integer;
  217. BEGIN
  218.   For loop1:=0 to 255 do
  219.     Getpal (loop1,pall[loop1,1],pall[loop1,2],pall[loop1,3]);
  220. END;
  221.  
  222. [C++]
  223.  
  224. void GrabPallette() {
  225.  
  226.   for(int loop1=0;loop1<256;loop1++)
  227.     GetPal(loop1,Pall2[loop1][0],Pall2[loop1][1],Pall2[loop1][2]);
  228.  
  229. }
  230.  
  231. This loads the entire pallette into variable pall. Then you must blackout
  232. the screen (see above), and draw what you want to screen without the
  233. construction being shown. Then what you do is go throgh the pallette. For
  234. each color, you see if the individual intensities are what they should be.
  235. If not, you increase them by one unit until they are. Beacuse intensites
  236. are in a range from 0 to 63, you only need do this a maximum of 64 times.
  237.  
  238. [Pascal]
  239.  
  240. Procedure Fadeup;
  241. VAR loop1,loop2:integer;
  242.     Tmp : Array [1..3] of byte;
  243.       { This is temporary storage for the values of a color }
  244. BEGIN
  245.   For loop1:=1 to 64 do BEGIN
  246.       { A color value for Red, green or blue is 0 to 63, so this loop only
  247.         need be executed a maximum of 64 times }
  248.     WaitRetrace;
  249.     For loop2:=0 to 255 do BEGIN
  250.       Getpal (loop2,Tmp[1],Tmp[2],Tmp[3]);
  251.       If Tmp[1]<Pall[loop2,1] then inc (Tmp[1]);
  252.       If Tmp[2]<Pall[loop2,2] then inc (Tmp[2]);
  253.       If Tmp[3]<Pall[loop2,3] then inc (Tmp[3]);
  254.         { If the Red, Green or Blue values of color loop2 are less then they
  255.           should be, increase them by one. }
  256.       Pal (loop2,Tmp[1],Tmp[2],Tmp[3]);
  257.         { Set the new, altered pallette color. }
  258.     END;
  259.   END;
  260. END;
  261.  
  262. [C++]
  263.  
  264. void Fadeup() {
  265.  
  266. //This is temporary storage for the values of a color
  267. unsigned char Tmp[3];
  268.  
  269.   // A color value for Red, green or blue is 0 to 63, so this loop only
  270.   // need be executed a maximum of 64 times.
  271.   for(int loop1=0;loop1<64;loop1++) {
  272.  
  273.     WaitRetrace();
  274.  
  275.     for(int loop2=0; loop2<256; loop2++) {
  276.       GetPal(loop2,Tmp[0],Tmp[1],Tmp[2]);
  277.  
  278.       // If the Red, Green or Blue values of color loop2 are less then they
  279.       // should be, increase them by one.
  280.       if ((Tmp[0] < Pall2[loop2][0]) && (Tmp[0] < 63)) Tmp[0]++;
  281.       if ((Tmp[1] < Pall2[loop2][1]) && (Tmp[1] < 63)) Tmp[1]++;
  282.       if ((Tmp[2] < Pall2[loop2][2]) && (Tmp[2] < 63)) Tmp[2]++;
  283.  
  284.       // Set the new, altered pallette color.
  285.       Pal (loop2,Tmp[0],Tmp[1],Tmp[2]);
  286.     }
  287.   }
  288. }
  289.  
  290. Hey-presto! The screen fades up. You can just add in a delay before the
  291. waitretrace if you feel it is too fast. Cool, no?
  292.  
  293.  
  294. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  295. ■  How do I fade out a screen?
  296.  
  297. This is just like the fade in of a screen, just in the opposite direction.
  298. What you do is you check each color intensity. If it is not yet zero, you
  299. decrease it by one until it is. BAAASIIIC!
  300.  
  301. [Pascal]
  302.  
  303. Procedure FadeDown;
  304. VAR loop1,loop2:integer;
  305.     Tmp : Array [1..3] of byte;
  306.       { This is temporary storage for the values of a color }
  307. BEGIN
  308.   For loop1:=1 to 64 do BEGIN
  309.     WaitRetrace;
  310.     For loop2:=0 to 255 do BEGIN
  311.       Getpal (loop2,Tmp[1],Tmp[2],Tmp[3]);
  312.       If Tmp[1]>0 then dec (Tmp[1]);
  313.       If Tmp[2]>0 then dec (Tmp[2]);
  314.       If Tmp[3]>0 then dec (Tmp[3]);
  315.         { If the Red, Green or Blue values of color loop2 are not yet zero,
  316.           then, decrease them by one. }
  317.       Pal (loop2,Tmp[1],Tmp[2],Tmp[3]);
  318.         { Set the new, altered pallette color. }
  319.     END;
  320.   END;
  321. END;
  322.  
  323. [C++]
  324.  
  325. void FadeDown() {
  326.  
  327. // This is temporary storage for the values of a color
  328. unsigned char Tmp[3];
  329.  
  330.   for(int loop1=0; loop1<64; loop1++) {
  331.  
  332.     WaitRetrace();
  333.  
  334.     for(int loop2=0; loop2<256; loop2++) {
  335.       GetPal(loop2,Tmp[0],Tmp[1],Tmp[2]);
  336.  
  337.       // If the Red, Green or Blue values of color loop2 are not yet zero,
  338.       // then, decrease them by one.
  339.       if (Tmp[0] > 0) Tmp[0]--;
  340.       if (Tmp[1] > 0) Tmp[1]--;
  341.       if (Tmp[2] > 0) Tmp[2]--;
  342.  
  343.       // Set the new, altered pallette color.
  344.       Pal(loop2,Tmp[0],Tmp[1],Tmp[2]);
  345.     }
  346.   }
  347. }
  348.  
  349. Again, to slow the above down, put in a delay above the WaitRetrace. Fading
  350. out the screen looks SO much more impressive then just clearing the screen;
  351. it can make a world of difference in the impression your demo etc will
  352. leave on the people viewing it. To restore the pallette, just do this :
  353.  
  354. [Pascal]
  355.  
  356. Procedure RestorePallette;
  357. VAR loop1:integer;
  358. BEGIN
  359.   WaitRetrace;
  360.   For loop1:=0 to 255 do
  361.     pal (loop1,Pall[loop1,1],Pall[loop1,2],Pall[loop1,3]);
  362. END;
  363.  
  364. [C++]
  365.  
  366. void RestorePallette() {
  367.  
  368.   WaitRetrace();
  369.   for(int loop1=0; loop1<255; loop1++)
  370.     Pal(loop1,Pall2[loop1][0],Pall2[loop1][1],Pall2[loop1][2]);
  371.  
  372. }
  373.  
  374. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  375. ■  In closing
  376.  
  377. Well, there are most of those origional questions answered ;-) The following
  378. sample program is quite big, so it might take you a while to get around it.
  379. Persevere and thou shalt overcome. Pallette manipulation has been a thorn
  380. in many coders sides for quite some time, yet hopefully I have shown you
  381. all how amazingly simple it is once you have grasped the basics.
  382.  
  383. I need more feedback! In which direction would you like me to head? Is there
  384. any particular section you would like more info on? Also, upload me your
  385. demo's, however trivial they might seem. We really want to get in contact
  386. with/help out new and old coders alike, but you have to leave us that message
  387. telling us about yourself and what you have done or want to do.
  388.  
  389. IS THERE ANYBODY OUT THERE!?!
  390.  
  391. P.S. Our new demo should be out soon ... it is going to be GOOOD ... keep
  392.      an eye out for it.
  393.  
  394.           [ And so she came across him, slumped over his keyboard
  395.             yet again . 'It's three in the morning' she whispered.
  396.             'Let's get you to bed'. He stirred, his face bathed in
  397.             the dull light of his monitor. He mutters something.
  398.             As she leans across him to disconnect the power, she
  399.             asks him; 'Was it worth it?'. His answer surprises her.
  400.             'No.' he says. In his caffiene-enduced haze, he smiles.
  401.             'But it sure is a great way to relax.'                  ]
  402.                                            - Grant Smith
  403.                                               Tue 13 July, 1993
  404.                                                2:23 am.
  405.  
  406. See you next week!
  407.    - Denthor
  408.  
  409.